/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dvdrental;

import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author wli
 */
@ManagedBean
@SessionScoped
public class FilmController {

    int startId;
    int endId;
    DataModel filmTitles;
    FilmHelper helper;
    private int recordCount = 1000;
    private int pageSize = 10;
    private Film current;
    private int selectedItemIndex;
    private String testName;
    private String testMessage;

    /** Creates a new instance of FilmController */
    public FilmController() {
          System.out.println("FilmController:FilmControllerAAAAAAAAAAAAAAA");
        helper = new FilmHelper();
        startId = 1;
        endId = 10;
    }

    public FilmController(int startId, int endId) {
          System.out.println("FilmController:FilmControllerBBBBBBBBBBBBBBBB");
        helper = new FilmHelper();
        this.startId = startId;
        this.endId = endId;
    }

    public String getTestMessage() {
        return testMessage;
    }

    public void setTestMessage(String testMessage) {
        this.testMessage = testMessage;
    }

    public String getTestName() {
        System.out.println("AAAAAAAAAAAAAAAAgetTestName");
        return testName;
    }

    public void setTestName(String testName) {
        System.out.println("BBBBBBBBBBBBBBBgetTestName");
        if (testName != null && testName.length() > 0) {
            testMessage = "Hello "+testName+" !";
        } else {
            testMessage = "Input is required!";
        }
        this.testName = testName;
    }
  
    public String testAction() {
        System.out.println(">>>>>>>>>>>>>"+current.getDescription());
        testMessage = current.getDescription();
        return "index4";
    }

  
    public Film getSelected() {
        if (current == null) {
            System.out.println("FilmController:getSelected:AAAAAAAAAAAA");
            current = helper.getFilmByID(1);
            selectedItemIndex = 1;
        }
        return current;
    }

    public DataModel getFilmTitles() {
        if (filmTitles == null) {
            filmTitles = new ListDataModel(helper.getFilmTitles(startId, endId));
        }
        System.out.println("FilmController:getFilmTitles");
        return filmTitles;
    }

    void recreateModel() {
        filmTitles = null;
    }

    public boolean isHasNextPage() {
        if (endId + pageSize <= recordCount) {
            return true;
        }
        return false;
    }

    public boolean isHasPreviousPage() {
        if (startId - pageSize > 0) {
            return true;
        }
        return false;
    }

    public String next() {
        startId = endId + 1;
        endId = endId + pageSize;
        recreateModel();
        return "index4";
    }

    public String previous() {
        startId = startId - pageSize;
        endId = endId - pageSize;
        recreateModel();
        return "index4";
    }

    public int getPageSize() {
        return pageSize;
    }

    public String prepareView() {
        System.out.println("FilmController:prepareView");
        current = (Film) getFilmTitles().getRowData();
        return "index4";
    }

    public String prepareList() {
        recreateModel();
        return "index4";
    }

    public String getLanguage() {
        int langID = current.getLanguageByLanguageId().getLanguageId().intValue();
        String language = helper.getLangByID(langID);
        return language;
    }

    public String getActors() {
        List actors = helper.getActorsByID(current.getFilmId());
        StringBuffer totalCast = new StringBuffer();
        for (int i = 0; i < actors.size(); i++) {
            Actor actor = (Actor) actors.get(i);
            totalCast.append(actor.getFirstName());
            totalCast.append(" ");
            totalCast.append(actor.getLastName());
            totalCast.append("  ");
        }
        return totalCast.toString();
    }

    public String getCategory() {
        Category category = helper.getCategoryByID(current.getFilmId());
        return category.getName();
    }
}
